<?php

class Users extends CI_Model {

    var $table_name = "users";
    var $data = NULL;

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    public function get_all()
    {
        $query = $this->db->get($this->table_name, 10);
        return $query->result();
    }

    public function get_user_by_email($email)
    {
        $query = $this->db->get_where($this->table_name, array('email' => $email), 1);
        return $query->result();
    }

    private function verified($nick)
    {
        $where = '(nick_name="'.$nick.'" OR email="'.$nick.'") AND active=1';
        $this->db->where($where);
        $query = $this->db->get($this->table_name, 1);
        if ($query->result() == NULL)
        {
            return FALSE;
        } 
        else 
        {
            $row = $query->result();
            $this->data = $row[0];
            return TRUE;
        }
    }

    public function login($nick, $password=NULL)
    {
        if($this->verified($nick) == TRUE)
        {
            if ($this->data->password == $password)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            } 
        }
    }

}
